package com.xuxin.baby.model;

import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.IsBuyBean;
import com.xuxin.baby.bean.VideoBean;
import com.xuxin.baby.bean.VideoCollectedBean;
import com.xuxin.baby.contract.VideoContract;
import com.xuxin.baby.network.Retrofit;
import com.xuxin.baby.network.RetrofitApi;
import com.xuxin.baby.network.myObserver;
import com.xuxin.baby.presenter.VideoPresenter;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Created by xu_xin
 * Created on 2020/11/2 0002
 * 视频播放
 */
public class VideoModel implements VideoContract.Model {
    @Override
    public void getVideo(VideoPresenter presenter, String tenantId, String token, String videoId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .getVideo(tenantId, token, "1", videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<VideoBean>() {
                    @Override
                    public void onNextX(@NonNull VideoBean videoBean) {
                        if (videoBean.isSuccess()) {
                            presenter.onSuccess(videoBean);
                        } else {
                            presenter.onError(videoBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void isCollected(VideoPresenter presenter, String tenantId, String token, String memberId, String videoId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .isCollected(tenantId, token, "1", memberId, videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<VideoCollectedBean>() {
                    @Override
                    public void onNextX(@NonNull VideoCollectedBean videoCollectedBean) {
                        if (videoCollectedBean.isSuccess()) {
                            presenter.onSuccess(videoCollectedBean);
                        } else {
                            presenter.onError(videoCollectedBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }

    @Override
    public void isBuy(VideoPresenter presenter, String tenantId, String token, String memberId, String videoId) {
        Retrofit.getInstance().createReq(RetrofitApi.class)
                .isBuy(tenantId, token, "1", memberId, videoId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new myObserver<IsBuyBean>() {
                    @Override
                    public void onNextX(@NonNull IsBuyBean isBuyBean) {
                        if (isBuyBean.isSuccess()) {
                            presenter.onSuccess(isBuyBean);
                        } else {
                            presenter.onError(isBuyBean);
                        }
                    }
                    @Override
                    public void onError(BaseBean baseBean) {
                        presenter.onError(baseBean);
                    }
                    @Override
                    public void onError(@NonNull Throwable e) {
                        presenter.onError(null);
                    }

                    @Override
                    public void onComplete() {
                        presenter.onComplete();
                    }
                });
    }
}